home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bash_114.zip / bash-1.14.2 / error.c < prev    next >
C/C++ Source or Header  |  1994-03-31  |  5KB  |  244 lines

  1. /* error.c -- Functions for handling errors. */
  2. /* Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GNU Bash, the Bourne Again SHell.
  5.  
  6.    Bash is free software; you can redistribute it and/or modify it under
  7.    the terms of the GNU General Public License as published by the Free
  8.    Software Foundation; either version 2, or (at your option) any later
  9.    version.
  10.  
  11.    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  12.    WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13.    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14.    for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License along
  17.    with Bash; see the file COPYING.  If not, write to the Free Software
  18.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  19.  
  20. #include <stdio.h>
  21. #include <fcntl.h>
  22.  
  23. #if defined (HAVE_VFPRINTF)
  24. #include <varargs.h>
  25. #endif
  26.  
  27. #include <errno.h>
  28. #if !defined (errno)
  29. extern int errno;
  30. #endif /* !errno */
  31.  
  32. #include "bashansi.h"
  33. #include "flags.h"
  34. #include "error.h"
  35. #include "command.h"
  36. #include "general.h"
  37.  
  38. extern int interactive_shell;
  39. extern char *dollar_vars[];
  40. extern char *shell_name;
  41. extern char *the_current_maintainer;
  42. #if defined (JOB_CONTROL)
  43. extern pid_t shell_pgrp;
  44. #endif /* JOB_CONTROL */
  45.  
  46. /* Return the name of the shell or the shell script for error reporting. */
  47. char *
  48. get_name_for_error ()
  49. {
  50.   char *name = (char *) NULL;
  51.  
  52.   if (!interactive_shell)
  53.     name = dollar_vars[0];
  54.   if (!name && shell_name && *shell_name)
  55.     name = base_pathname (shell_name);
  56.   if (!name)
  57.     name = "bash";
  58.  
  59.   return (name);
  60. }
  61.  
  62. /* Report an error having to do with FILENAME. */
  63. void
  64. file_error (filename)
  65.      char *filename;
  66. {
  67.   report_error ("%s: %s", filename, strerror (errno));
  68. }
  69.  
  70. #if !defined (HAVE_VFPRINTF)
  71. void
  72. programming_error (reason, arg1, arg2, arg3, arg4, arg5)
  73.      char *reason;
  74. {
  75. #if defined (JOB_CONTROL)
  76.   give_terminal_to (shell_pgrp);
  77. #endif /* JOB_CONTROL */
  78.  
  79.   report_error (reason, arg1, arg2);
  80.   fprintf (stderr, "Report this to %s\n", the_current_maintainer);
  81.   fprintf (stderr, "Stopping myself...");
  82.   fflush (stderr);
  83.   abort ();
  84. }
  85.  
  86. void
  87. report_error (format, arg1, arg2, arg3, arg4, arg5)
  88.      char *format;
  89. {
  90.   fprintf (stderr, "%s: ", get_name_for_error ());
  91.  
  92.   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  93.   fprintf (stderr, "\n");
  94.   if (exit_immediately_on_error)
  95.     exit (1);
  96. }  
  97.  
  98. void
  99. fatal_error (format, arg1, arg2, arg3, arg4, arg5)
  100.      char *format;
  101. {
  102.   fprintf (stderr, "%s: ", get_name_for_error ());
  103.  
  104.   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  105.   fprintf (stderr, "\n");
  106.  
  107.   exit (2);
  108. }
  109.  
  110. void
  111. internal_error (format, arg1, arg2, arg3, arg4, arg5)
  112.      char *format;
  113. {
  114.   fprintf (stderr, "%s: ", get_name_for_error ());
  115.  
  116.   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  117.   fprintf (stderr, "\n");
  118. }
  119.  
  120. #else /* We have VARARGS support, so use it. */
  121.  
  122. void
  123. programming_error (va_alist)
  124.      va_dcl
  125. {
  126.   va_list args;
  127.   char *format;
  128.  
  129. #if defined (JOB_CONTROL)
  130.   give_terminal_to (shell_pgrp);
  131. #endif /* JOB_CONTROL */
  132.  
  133.   va_start (args);
  134.   format = va_arg (args, char *);
  135.   vfprintf (stderr, format, args);
  136.   fprintf (stderr, "\n");
  137.   va_end (args);
  138.  
  139.   fprintf (stderr, "Tell %s to fix this someday.\n", the_current_maintainer);
  140.   fprintf (stderr, "Stopping myself...");
  141.   fflush (stderr);
  142.   abort ();
  143. }
  144.  
  145. void
  146. report_error (va_alist)
  147.      va_dcl
  148. {
  149.   va_list args;
  150.   char *format;
  151.  
  152.   fprintf (stderr, "%s: ", get_name_for_error ());
  153.   va_start (args);
  154.   format = va_arg (args, char *);
  155.   vfprintf (stderr, format, args);
  156.   fprintf (stderr, "\n");
  157.  
  158.   va_end (args);
  159.   if (exit_immediately_on_error)
  160.     exit (1);
  161. }
  162.  
  163. void
  164. fatal_error (va_alist)
  165.      va_dcl
  166. {
  167.   va_list args;
  168.   char *format;
  169.  
  170.   fprintf (stderr, "%s: ", get_name_for_error ());
  171.   va_start (args);
  172.   format = va_arg (args, char *);
  173.   vfprintf (stderr, format, args);
  174.   fprintf (stderr, "\n");
  175.  
  176.   va_end (args);
  177.   exit (2);
  178. }
  179.  
  180. void
  181. internal_error (va_alist)
  182.      va_dcl
  183. {
  184.   va_list args;
  185.   char *format;
  186.  
  187.   fprintf (stderr, "%s: ", get_name_for_error ());
  188.   va_start (args);
  189.   format = va_arg (args, char *);
  190.   vfprintf (stderr, format, args);
  191.   fprintf (stderr, "\n");
  192.  
  193.   va_end (args);
  194. }
  195.  
  196. itrace (va_alist)
  197.      va_dcl
  198. {
  199.   va_list args;
  200.   char *format;
  201.  
  202.   fprintf(stderr, "TRACE: pid %d: ", getpid());
  203.   va_start (args);
  204.   format = va_arg (args, char *);
  205.   vfprintf (stderr, format, args);
  206.   fprintf (stderr, "\n");
  207.  
  208.   va_end (args);
  209.  
  210.   fflush(stderr);
  211. }
  212.  
  213. #if 0
  214. /* A trace function for silent debugging -- doesn't require a control
  215.    terminal. */
  216. trace (va_alist)
  217.      va_dcl
  218. {
  219.   va_list args;
  220.   char *format;
  221.   static FILE *tracefp = (FILE *)NULL;
  222.  
  223.   if (tracefp == NULL)
  224.     tracefp = fopen("/usr/tmp/bash-trace.log", "a+");
  225.  
  226.   if (tracefp == NULL)
  227.     tracefp = stderr;
  228.   else
  229.     fcntl (fileno (tracefp), F_SETFD, 1);     /* close-on-exec */
  230.  
  231.   fprintf(tracefp, "TRACE: pid %d: ", getpid());
  232.  
  233.   va_start (args);
  234.   format = va_arg (args, char *);
  235.   vfprintf (tracefp, format, args);
  236.   fprintf (tracefp, "\n");
  237.  
  238.   va_end (args);
  239.  
  240.   fflush(tracefp);
  241. }
  242. #endif
  243. #endif /* HAVE_VFPRINTF */
  244.